<?php

namespace App\Http\Controllers;

use App\Student;
use Illuminate\Http\Request;

class StudentController extends Controller
{
    //学生列表页
    public function index()
    {
        $students = Student::paginate(10);

        return view('student/index', [
            'students' => $students
        ]);
    }

    public function create(Request $request)
    {
        $student = new Student();

        if ($request->isMethod('POST')) {

            //1、控制器表单验证
            $this->validate($request, [
                'Student.name' => 'required|min:2|max:20',
                'Student.age' => 'required|integer',
                'Student.sex' => 'required|integer',
            ], [
                'required' => ':attribute 为必填项',
                'min' => ':attribute 长度部符合要求',
                'integer' => ':attribute 只能为整数'
            ], [
                'Student.name' => '姓名',
                'Student.age' => '年龄',
                'Student.sex' => '性别'
            ]);

            //2、validator类验证


            $data = $request->input('Student');
            if (Student::create($data)) {
                return redirect('student/index')->with('success', '添加成功');
            } else {
                return redirect()->back();
            }
        }
        return view('student/create', [
            'student' => $student
        ]);
    }

    public function update(Request $request, $id)
    {
        $student = Student::find($id);
        if ($request->isMethod('POST')) {
            $data = $request->input('Student');
            $student->name = $data['name'];
            $student->age = $data['age'];
            $student->sex = $data['sex'];

            if ($student->save()) {
                return redirect('student/index')->with('success', '修改成功-', $id);
            }
        }
        return view('student/update', [
            'student' => $student
        ]);

    }

    public function detail($id)
    {
        $student = Student::find($id);
        return view('student/detail', [
            'student' => $student
        ]);
    }

    public function delete($id)
    {
        $student=Student::find($id);
        if($student->delete()){
            return redirect('student/index')->with('success','删除成功-'.$student->id);
        }else{
            return redirect('student/index')->with('success','删除失败-'.$student->id);
        }
    }

//    public function save(Request $request)
//    {
//        $data=$request->input('Student');
//        $student=new Student();
//        $student->name=$data['name'];
//        $student->age=$data['age'];
//        $student->sex=$data['sex'];
//
//        if($student->save()){
//            return redirect('student/index');
//        }else{
//            return redirect()->back();
//        }
//    }
}